package com.demo.demo.controller;

import com.demo.demo.bean.*;
import com.demo.demo.dao.InventoryInDao;
import com.demo.demo.dao.InventoryOutDao;
import com.demo.demo.dao.InventoryRecordDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inventory")
public class InventoryController {

    @Autowired
    private InventoryInDao inventoryInDao;

    @Autowired
    private InventoryOutDao inventoryOutDao;

    @Autowired
    private InventoryRecordDao inventoryRecordDao; // 新增此行注入

    @PostMapping("/in")
    public ResponseResult addInventoryIn(@RequestBody InventoryIn inventoryIn) {
        try {
            inventoryInDao.insertInventoryIn(inventoryIn);
            return new ResponseResult(200, "入库成功", null,0);
        } catch (Exception e) {
            return new ResponseResult(500, "入库失败：" + e.getMessage(), null,0);
        }
    }

    @PostMapping("/out")
    public String addInventoryOut(@RequestBody InventoryOut inventoryOut) {
        inventoryOutDao.insertInventoryOut(inventoryOut);
        return "ok";
    }

    @GetMapping("/records")
    public PageInfo<InventoryRecord> getInventoryRecordsByGet(
        @RequestParam(required = false) String varietyType,
        @RequestParam(required = false) String location,
        @RequestParam(defaultValue = "1") int pageNum,
        @RequestParam(defaultValue = "10") int pageSize) {

        int pageStart = (pageNum - 1) * pageSize;

        List<InventoryRecord> records = inventoryRecordDao.findRecordsByCriteria(varietyType, location, pageStart, pageSize);
        int total = inventoryRecordDao.countRecordsByCriteria(varietyType, location);

        return new PageInfo<>(records, total, pageNum, pageSize);
    }


}
